package cn.wen.eeayjoke.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import cn.wen.core.utils.APPUtils;

/**
 * SharedPreferences工具类
 *
 * @author Administrator
 */
public class SPUtils {
    private static final String CONFIG = "CONFIG";

    //CT 尽量别用　
    public static SharedPreferences getSharedPreferences() {
        return APPUtils.getContext().getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }

    //CT 建议使用　
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
    }

    public static void clear(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().clear().commit();
    }

    public static void clearPutString(Context context, String key) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(key, "").commit();
    }

    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putBoolean(key, value).commit();
    }

    public static void putBoolean(String key, boolean value) {
        putBoolean(APPUtils.getContext(), key, value);
    }

    private static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return getBoolean(APPUtils.getContext(), key, defValue);
    }

    public static boolean getBoolean(String key) {
        return getBoolean(APPUtils.getContext(), key, false);
    }

    private static void putString(Context context, String key, String value) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(key, value).commit();
    }

    public static void putString(String key, String value) {
        putString(APPUtils.getContext(), key, value);
    }

    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(key, defValue);
    }

    public static String getString(String key, String defValue) {
        return getString(APPUtils.getContext(), key, defValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences sp = getSharedPreferences(APPUtils.getContext());
        sp.edit().putInt(key, value).commit();
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defValue) {
        SharedPreferences sp = getSharedPreferences(APPUtils.getContext());
        return sp.getInt(key, defValue);
    }

    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt(key, defValue);
    }

    public static void putDouble(Context context, String key, double value) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putFloat(key, (float) value).commit();
    }

    public static double getDouble(Context context, String key, double defValue) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getFloat(key, (float) defValue);
    }

    public static void putObject(final String key, final Object obj) {
        if (obj == null) {
            return;
        }
        putString(APPUtils.getContext(), key, new Gson().toJson(obj).toString());
    }

    public static <T> T getObject(String key, Class<T> clazz) {
        try {
            String json = getString(APPUtils.getContext(), key, "");
            if (TextUtils.isEmpty(json)) {
                return null;
            }
            Gson gson = new Gson();
            return gson.fromJson(json, clazz);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }


}
